package com.bbzn.device.client.service.emq.order.impl.mq;

import com.alibaba.fastjson.JSONObject;
import com.bbzn.device.client.constant.CusFenConstant;
import com.bbzn.device.client.constant.SosConstant;
import com.bbzn.device.client.dataobject.Sos;
import com.bbzn.device.client.dataobject.SystemParam;
import com.bbzn.device.client.exception.DefinitException;
import com.bbzn.device.client.service.SosService;
import com.bbzn.device.client.service.SystemParamByCacheService;
import com.bbzn.device.client.utils.OptionalBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @Author wulongbo
 * @Date 2021/12/16 16:19
 * @Version 1.0
 */
@Component
public class MqSetSosImpl extends BaseMqSendImpl {

    @Autowired
    private SystemParamByCacheService systemParamByCacheService;

    @Autowired
    private SosService sosService;

    @Override
    public String checkMap(String language) throws Exception {
        if (!OptionalBean.ofNullable(map.get("phone")).isPresent()) {
            SystemParam systemParam = systemParamByCacheService.getValues(SosConstant.SOS_ENUM, SosConstant.SOS_NO_PHONE_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        if (!OptionalBean.ofNullable(map.get("name")).isPresent()) {
            SystemParam systemParam = systemParamByCacheService.getValues(SosConstant.SOS_ENUM, SosConstant.SOS_NO_NAME_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        if (!OptionalBean.ofNullable(map.get("isOpen")).isPresent()) {
            SystemParam systemParam = systemParamByCacheService.getValues(CusFenConstant.CUS_FEN_ENUM, CusFenConstant.UN_IS_OPEN_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }

        Sos sos = new Sos();
        if (OptionalBean.ofNullable(map.get("id")).isPresent()) {
            sos.setId(Long.valueOf(String.valueOf(map.get("id"))));
        }
        sos.setImei(map.get("imei").toString());
        sos.setPhone(map.get("phone").toString());
        sos.setName(map.get("name").toString());
        sos.setIsOpen(Integer.parseInt(map.get("isOpen").toString()));
        sos.setCreateTime(new Date());
        sosService.insertOrUpdateSelective(sos);
        return JSONObject.toJSONString(map);
    }
}
